<?php
class ManageAction extends GlobalAction{
	//地区管理
    public function _initialize(){ 			
			parent::_initialize(); 
			LC('@.Help.order');		 
			LC('@.Lib.Json'); 
			LC('@.Help.transaction');
	       
	}
    //获取地址;
	public function ajaxsearch(){
		header('Content-type: text/html; charset=' . CHARSET);

		$type   = !empty($_REQUEST['type'])   ? intval($_REQUEST['type'])   : 0;
		$parent = !empty($_REQUEST['parent']) ? intval($_REQUEST['parent']) : 0;

		$arr['regions'] = get_regions($type, $parent);
		$arr['type']    = $type;
		$arr['target']  = !empty($_REQUEST['target']) ? stripslashes(trim($_REQUEST['target'])) : '';
		$arr['target']  = htmlspecialchars($arr['target']);

		$json = new JSON;
		echo $json->encode($arr);
	}
	//保存用户送货地址;
	public function save_address(){
		$json  = new JSON;
		$add = $json->decode($_POST['add']);
		$add=(array)$add;
		
		$consignee = array(
					'address_id'    => empty($add['address_id']) ? 0  : intval($add['address_id']),
					'consignee'     => empty($add['consignee'])  ? '' : trim($add['consignee']),
					'country'       => empty($add['country'])    ? '' : $add['country'],
					'province'      => empty($add['province'])   ? '' : $add['province'],
					'city'          => empty($add['city'])       ? '' : $add['city'],
					'district'      => empty($add['district'])   ? '' : $add['district'],
					'email'         => empty($add['email'])      ? '' : $add['email'],
					'address'       => empty($add['address'])    ? '' : $add['address'],
					'zipcode'       => empty($add['zipcode'])    ? '' : make_semiangle(trim($add['zipcode'])),
					'tel'           => empty($add['tel'])        ? '' : make_semiangle(trim($add['tel'])),
					//'mobile'        => empty($add['mobile'])     ? '' : make_semiangle(trim($add['mobile'])),
					'sign_building' => empty($add['sign_building']) ? '' : $add['sign_building'],
					'best_time'     => empty($add['best_time'])  ? '' : $add['best_time'],
				);
		// dump($_SESSION);
        if ($_SESSION['user_id'] > 0){
           
            /* 如果用户已经登录，则保存收货人信息 */
            $consignee['user_id'] = $_SESSION['user_id'];
			 
            save_consignee($consignee, true);
        }
		$address_id=$consignee['address_id'];		
		$json  = new JSON;
		$rs['error']= 0;
		$rs['message']=$this->get_address($address_id);


		 /* 保存到session */
        $_SESSION['flow_consignee'] = stripslashes_deep($consignee);
		die($json->encode($rs));	
	}
	//显示单条地址;
	public function show_one_address(){		 
		$address_id=getgp("id");		 
		$json  = new JSON;
		$rs['error']= 0;
		$rs['message']=$this->get_address($address_id);
		die($json->encode($rs));	
		 
	}
	public function get_address($address_id){
 
		$_CFG=G('cfg');
	  /* 取得国家列表、商店所在国家、商店所在国家的省列表 */
        $this->assign('country_list',       get_regions());		 
        $this->assign('shop_country',       $_CFG['shop_country']);
        $this->assign('shop_province_list', get_regions(1, $_CFG['shop_country']));
         /* 获得用户所有的收货人信息 */
        if ($_SESSION['user_id'] > 0){
            $consignee_list = get_consignee_list($_SESSION['user_id']);            
        }        
        //$this->assign('name_of_region',   array($_CFG['name_of_region_1'], $_CFG['name_of_region_2'], $_CFG['name_of_region_3'], $_CFG['name_of_region_4']));
      // dump($consignee_list);
		$this->assign('consignee_list', $consignee_list);
	    if($address_id==0||empty($address_id)){
			$address_id=count($consignee_list)-1;
		}		 
	//dump($consignee_list);
		for($i=0;$i<count($consignee_list);$i++){		 
			  if($consignee_list[$i]['address_id']==$address_id){				 
				 $address=$consignee_list[$i];
			  }		
		}
		//dump($address);
		//exit;
         /* 取得每个收货地址的省市区列表 */
        $province_list = array();
        $city_list = array();
        $district_list = array();
       
		$province_list = get_regions(1, $address['country']);
        $city_list     = get_regions(2, $address['province']);
        $district_list = get_regions(3, $address['city']);
		 
        $this->assign('province_list', $province_list);
        $this->assign('city_list',     $city_list);
        $this->assign('district_list', $district_list);
		$this->assign("uadd",$address);

        /* 返回收货人页面代码 */
        $this->assign('real_goods_count', exist_real_goods(0, $flow_type) ? 1 : 0);
		$is_show=count($consignee_list)<5?0:1;
		 
        $this->assign('is_show',$is_show);
		$this->display("user_address");
		$content = ob_get_contents();
        ob_end_clean();	
		return $content;
	}
    //选配送方式
	public function select_shipping(){	
		$json = new JSON;
		$result = array('error' => '', 'content' => '', 'need_insure' => 0);

		/* 取得购物类型 */
		$flow_type = 0;

		/* 获得收货人信息 */
		$consignee = get_consignee($_SESSION['user_id']);

		/* 对商品信息赋值 */
		$cart_goods = cart_goods($flow_type); // 取得商品列表，计算合计

		if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
		{
			$result['error'] = $_LANG['no_goods_in_cart'];
		}
		else
		{
			/* 取得购物流程设置 */
			$smarty->assign('config', $_CFG);

			/* 取得订单信息 */
			$order = flow_order_info();

			$order['shipping_id'] = intval($_REQUEST['shipping']);
			$regions = array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district']);
			$shipping_info = shipping_area_info($order['shipping_id'], $regions);

			/* 计算订单的费用 */
			$total = order_fee($order, $cart_goods, $consignee);
			$this->assign('total', $total);

			/* 取得可以得到的积分和红包 */
			$this->assign('total_integral', cart_amount(false, $flow_type) - $total['bonus'] - $total['integral_money']);
			$this->assign('total_bonus',    price_format(get_total_bonus(), false));

			/* 团购标志 */
			if ($flow_type == CART_GROUP_BUY_GOODS)
			{
				$smarty->assign('is_group_buy', 1);
			}

			$result['cod_fee']     = $shipping_info['pay_fee'];
			if (strpos($result['cod_fee'], '%') === false)
			{
				$result['cod_fee'] = price_format($result['cod_fee'], false);
			}
			$result['need_insure'] = ($shipping_info['insure'] > 0 && !empty($order['need_insure'])) ? 1 : 0;
			    $this->display("user_address");
				$content = ob_get_contents();
			    ob_end_clean();	
			$result['content']     =$content;// $smarty->fetch('library/order_total.lbi');
		}
		
	 

		echo $json->encode($result);
		exit;
		
	
	}

}

?>